from random import randint
from time import sleep

from selenium import webdriver
from selenium.webdriver.support.select import Select

driver = webdriver.Firefox()
# 打开登录页面
driver.get('http://localhost/xiaoyulianxi/index.asp')

# 完成登录
ipts = driver.find_elements_by_tag_name('input')
ipts[0].send_keys('admin')
ipts[1].send_keys('admin')
ipts[2].click()
sleep(2)

#选择左边框架定位系统设置
driver.switch_to.frame('left')

#系统单位CSS
xitongset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
driver.find_element_by_css_selector(xitongset_css).click()

#计量单位CSS
jldw_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
driver.find_element_by_css_selector(jldw_css).click()
sleep(1)

#返回默认位置在选择右边框架
driver.switch_to.default_content()
sleep(1)
driver.switch_to.frame('right')

#定位元素并点击删除单位
driver.find_element_by_id('chkall').click()
sc_css = 'input.button:nth-child(2)'
driver.find_element_by_css_selector(sc_css).click()
sleep(1)
driver.switch_to.alert.accept()

#定位添加计量单位
driver.find_element_by_class_name('button').click()
sleep(1)

#在输入框内输入计量单位KG
driver.find_element_by_name('danwei').send_keys('KG')
sleep(1)

#并点击确定
driver.find_element_by_name('submit').click()
driver.switch_to.alert.accept()
sleep(1)

# 返回默认位置选择左边框架定位产品大类管理
driver.switch_to.default_content()
sleep(1)
driver.switch_to.frame('left')

#产品大类CSS
cpdl_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
driver.find_element_by_css_selector(cpdl_css).click()
sleep(1)

#返回默认位置选择右边框架定位添加小类
driver.switch_to.default_content()
sleep(1)
driver.switch_to.frame('right')
tjdl_css ='input.button:nth-child(1)'
driver.find_element_by_css_selector(tjdl_css).click()
sleep(1)

#在输入框内输入生活用品
driver.find_element_by_name('bigclass').send_keys('生活用品')
sleep(1)

#确定
#确定css
qd_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(qd_css).click()
sleep(1)
driver.switch_to.alert.accept()

#返回默认位置选择产品小类管理、
driver.switch_to.default_content()
sleep(1)
driver.switch_to.frame('left')

#产品小类CSS
cpxl_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
driver.find_element_by_css_selector(cpxl_css).click()
sleep(1)

#返回默认位置选择右边框架
driver.switch_to.default_content()
sleep(1)
driver.switch_to.frame('right')

#添加小类 小类CSS
tjxl_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(tjxl_css).click()
sleep(1)

#定位选择大类并随机选择小类
bigclass = driver.find_element_by_name('bigclass')
list = Select(bigclass)
list_count = len(list.options)
rand = randint(0, list_count-1)
list.select_by_index(rand)

#定位输入框输入矿泉水
driver.find_element_by_name('smallclass').send_keys('矿泉水')
sleep(1)

#确认CSS
qr_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(qr_css).click()
sleep(1)
driver.switch_to.alert.accept()

#返回默认位置选择顶部框架定位退出系统
driver.switch_to.default_content()
driver.switch_to.frame('topFrame')
driver.find_element_by_link_text('安全退出').click()
sleep(1)
driver.switch_to.alert.accept()
sleep(1)

#关闭浏览器
driver.quit()




